package org.one.stone.soup.grfxML;

import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.xml.XmlElement;
  /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public class DataPolygon extends DataGeometry implements ITag {
	public static String DEFAULT_ID = "_Polygon";
		DataState closed = new DataState(true);
		DataPoints points = new DataPoints();
		DataPoints pointsView = new DataPoints();
		DataSize thickness = new DataSize(1);
		DataSize thicknessView = new DataSize(1);

		GeometryPolygon _polygon = null;
		DataTranslation _translation;
public DataPolygon() {
	try{
		setId(DEFAULT_ID,this);
	}
	catch(LockException le){} //Dummy catch
}
public void buildFromgrfxML(XmlElement xml)
{

	String name = xml.getAttributeValueByName(Token.ID);
	if(name!=null)
	{
		try{
			setId(name,this);
		}
		catch(LockException le){} //Dummy catch
	}

	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.CLOSED))
		{
	  		closed = new DataState(true);
	 		closed.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.POINTS))
		{
			points = new DataPoints();
			points.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.THICKNESS))
		{
			thickness = new DataSize(TagModifier.SIZE_TYPE_THICKNESS);
			thickness.buildFromgrfxML(element,TagModifier.SIZE_TYPE_THICKNESS);
		}
		else if(super.buildFromElement(element)==false && !NameHelper.isSystemName(element.getName()))
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.POLYGON,""+element.getStartLineNo()} );
		}
	}
}
public void copyFromgrfxML(XmlElement xml)
{
	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.CLOSED))
		{
	 		closed.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.POINTS))
		{
			points.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.THICKNESS))
		{
			thickness.copyFromgrfxML(element,TagModifier.SIZE_TYPE_THICKNESS);
		}
		else if(super.copyFromElement(element)==false)
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.POLYGON,""+element.getStartLineNo()} );
		}
	}
}
public DataGeometry getClone() {

	DataPolygon newPolygon = new DataPolygon();

	super.getClonedElements(newPolygon);

	newPolygon.closed = closed.getClone();
	newPolygon.points = points.getClone();
	newPolygon.thickness = thickness.getClone();

	return newPolygon;
}
public DataGeometry getClone(String path) {

	DataPolygon newPolygon = new DataPolygon();

	GrfxMLStoreProcessor.setCloneName( path,this,newPolygon );

	super.getClonedElements(path,newPolygon);

	newPolygon.closed = closed.getClone(path);
	newPolygon.points = points.getClone(path);
	newPolygon.thickness = thickness.getClone(path);

	return newPolygon;
}
public DataState getClosed() {
	return closed;
}
public Geometry getGeometry(GrfxMLEngine engine)
{
	if(_polygon==null && _translation!=null && color!=null)
	{
		_polygon = new GeometryPolygon(
				engine,
				_translation.window,
				bounds,
				_translation.visibleView,
				transparency,
				color,
				filled,
				closed,
				_translation.positionView,
				offsetView,
				pointsView,
				thicknessView
				);
		if( !NameHelper.isSystemName(getId()) )
		{
			_polygon.setId(getId());
		}
	}

	return(_polygon);
}
public DataPoints getPoints() {
	return points;
}

public DataSize getThickness()
{
	return thickness;
}

public java.awt.Polygon getPolygon() {
	return _polygon.polygon;
}
public void register(DataLinker store) {

	super.register(store);

	try{
		store.add(closed,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(thickness,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(points,this);
	}
	catch(LockException le)
	{}//Dummy catch
	points.register(store);
}
public void replace(Field oldObj, Field newObj) {
	replace(oldObj,newObj,false);
}
public void replace(Field oldObj, Field newObj,boolean recursive) {

	_polygon = null;

	super.replace(oldObj,newObj);

	if(closed==oldObj)
		closed=(DataState)newObj;

	if(points==oldObj)
		points=(DataPoints)newObj;

	if(thickness==oldObj)
		thickness=(DataSize)newObj;

	if(recursive)
	{
		points.replace(oldObj,newObj,recursive);
	}
}
public void resetChanged() {
	super.resetChanged();
	closed.resetChanged();
	points.resetChanged();
	pointsView.resetChanged();
	thickness.resetChanged();
}
public void setClosed(DataState newClosed) {
	closed = newClosed;
}
public void setPoints(DataPoints newPoints) {
	points = newPoints;
}
public String togrfxML(String name) {
	String xml = "<" + name;

	if(!NameHelper.isSystemName(getId()))
	{
		xml += " id=\""+getId()+"\"";
	}

	xml += ">";
	xml += super.togrfxML(name);
	xml += closed.togrfxML(DataState.TRUE,Token.CLOSED);
	xml += points.togrfxML(Token.POINTS);
	xml += thickness.togrfxML(new DataSize(1),Token.THICKNESS,TagModifier.SIZE_TYPE_THICKNESS);
	xml += "</" + name + ">\n";
	return xml;
}
public String toString()
{
	if(GrfxMLEngine.DEBUG)
		return(togrfxML( DEFAULT_ID.substring(1) ));

	String xml = "<"+DEFAULT_ID.substring(1)+"/>";

	return xml;
}
public void translate(DataTranslation t)
{

	if(_translation==null)
		_translation=t;

	try{
		GrfxMLProcessor.translate(points,TagModifier.POINT_TYPE_POINT,t,pointsView);
	}
	catch(LockException le)
	{
		le.printStackTrace();
	}

	try{
		GrfxMLProcessor.translate(thickness,TagModifier.SIZE_TYPE_THICKNESS,t,thicknessView);
	}
	catch(LockException le)
	{
		le.printStackTrace();
	}

	super.translate(t);
}
}
